/*
 * Copyright (c) 2017. 杭州端点网络科技有限公司.  All rights reserved.
 */

package com.jiantong.user.impl.manager;

import com.jiantong.user.impl.dao.DepartmentUserDao;
import com.jiantong.user.impl.dao.OrganizationUserDao;
import com.jiantong.user.impl.dao.UserDao;
import com.jiantong.user.impl.dao.UserProfileDao;
import com.jiantong.user.model.DepartmentUser;
import com.jiantong.user.model.OrganizationUser;
import com.jiantong.user.model.User;
import com.jiantong.user.model.UserProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @author : panxin
 */
@Component
public class UserManager {

    private final UserDao userDao;
    private final UserProfileDao userProfileDao;
    private final OrganizationUserDao organizationUserDao;
    private final DepartmentUserDao departmentUserDao;

    @Autowired
    public UserManager(UserDao userDao,UserProfileDao userProfileDao, OrganizationUserDao organizationUserDao, DepartmentUserDao departmentUserDao) {
        this.userDao = userDao;
        this.userProfileDao = userProfileDao;
        this.organizationUserDao = organizationUserDao;
        this.departmentUserDao = departmentUserDao;
    }

    @Transactional
    public Boolean createOrganizationUser(User user, OrganizationUser organizationUser) {
        userDao.create(user);

        organizationUser.setUserId(user.getId());
        organizationUserDao.create(organizationUser);

        return user.getId() != null;
    }

    @Transactional
    public Boolean createDepartmentUser(User user, DepartmentUser departmentUser) {
        userDao.create(user);

        departmentUser.setUserId(user.getId());
        departmentUserDao.create(departmentUser);

        return user.getId() != null;
    }

    /**
     * 更新个人资料 养殖端
     * @param id
     * @param avatarMap
     * @param realName
     * @param gender
     * @param identityCardNumber
     * @param email
     * @param corporateCharterMap
     * @return 是否成功
     */
    @Transactional
    public Boolean updatePersonalData(Long id,Map<String , Map<String , String>> avatarMap, String realName, Integer gender, String identityCardNumber, String email, Map<String , Map<String , String>> corporateCharterMap){
       //email 在parana_users 表中更新    根据ID更新
        User user = new User();
        user.setId(id);
        user.setEmail(email);
        Boolean flag = userDao.update(user);

       //avatar realName  gender identityCardNumber    在userProfiles表中更新 根据user_id 更新
        UserProfile userProfile = new UserProfile();
        userProfile.setAvatarMap(avatarMap);
        userProfile.setUserId(id);
        userProfile.setRealName(realName);
        userProfile.setGender(gender);
        userProfile.setIdentityCardNumber(identityCardNumber);
        userProfileDao.updateByUserId(userProfile);

       //corporateCharter 只有养殖端才有(需要区别对待,政府端可以不执行养殖端必须执行) 在djt_organization_users 表中更新  根据user_id更新
        OrganizationUser organizationUser = new OrganizationUser();
        organizationUser.setCorporateCharterMap(corporateCharterMap);
        organizationUser.setUserId(id);
        organizationUserDao.updateByUserId(organizationUser);
       return flag;
    }
    /**
     * 更新个人资料   政府端
     * @param id
     * @param avatarMap
     * @param realName
     * @param gender
     * @param identityCardNumber
     * @param email
     * @return 是否成功
     */
    @Transactional
    public Boolean updatePersonalData(Long id,Map<String , Map<String , String>> avatarMap, String realName, Integer gender, String identityCardNumber, String email){

        Boolean aBoolean1 = false;
       //email 在parana_users 表中更新    根据ID更新
        User user = new User();
        user.setId(id);
        user.setEmail(email);
        Boolean aBoolean = userDao.update(user);

        UserProfile userProfile = new UserProfile();
        userProfile.setAvatarMap(avatarMap);
        userProfile.setUserId(id);
        userProfile.setRealName(realName);
        userProfile.setGender(gender);
        userProfile.setIdentityCardNumber(identityCardNumber);
        userProfileDao.updateByUserId(userProfile);

        return aBoolean;

    }
}
